const app = getApp();
const {
    stageList,
    sloganDefault,
    stageindexDefault,
} = require("../../config")
let openid;
Page({

    // 页面的初始数据
    data: {
        isShowUserName: false,
        userInfo: {
            stageindex: stageindexDefault,
            slogan: sloganDefault,
        },
        userInfoJson: '',
        openid: "",
        // currentAccordionList: [],
        activeNames: [],
        slogan: '做个爱学习的好孩子',
        stageList,
    },

    // button获取用户信息
    onGotUserInfo: function (e) {
        app.getOpenid();

        if (e.detail.userInfo) {
            let user = e.detail.userInfo;
            let userInfoJson = JSON.stringify(user);
            console.log(userInfoJson, '20-20 me.js:::::userInfoJson 20-20 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++');
            this.setData({
                isShowUserName: true,
                // userInfo: e.detail.userInfo,
                userInfo: {...that.data.userInfo, ...e.detail.userInfo},
                userInfoJson,
                openid: getApp().globalData.openid,
            })
            user.openid = app.globalData.openid;
            app._saveUserInfo(user);
        } else {
            app._showSettingToast('登陆需要允许授权');
        }

    },
    loginOut() {
        app._saveUserInfo(null)
        this.setData({
            isShowUserName: false
        })
    },



    //生命周期函数--监听页面加载
    onLoad: function (options) {
        app.globalData.slogan = sloganDefault;
        app.globalData.stageindex = sloganDefault;
        openid = app.globalData.openid
        console.log("openid", openid)
        var that = this;
        that.setData({
            openid: openid
        })
        
        var user = app.globalData.userInfo;
        if (user) {
            const userInfoJson = JSON.stringify(user);
            that.setData({
                userInfoJson,
                isShowUserName: true,
                userInfo: {...that.data.userInfo, ...user},
            })
            // 手风琴示例代码
            this.key = String(Math.floor(Math.random() * 3))
        } else {
            // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
            // 所以此处加入 callback 以防止这种情况
            app.userInfoReadyCallback = res =>
            {
                const userInfoJson = JSON.stringify(res.user);
                that.setData({
                    userInfoJson,
                    // userInfo: res.userInfo,
                    userInfo: {...that.data.userInfo, ...res.userInfo},
                    isShowUserName: true
                })
            }
            // 手风琴示例代码
            this.key = String(Math.floor(Math.random() * 3))
        }

        // 不做本地缓存了， onShow 从数据库中读
        
    },

    onShow(){
        const that = this;
        const app = getApp();
        const openid = this.data.openid;
        console.log(this.data, '83-83 me.js:::::onShow:::this.data:');
        if(!openid) return;
        app.globalData.userDoc.doc(openid).get({
            success(res){
                console.log('77-77 me.js:::::onShow:::success::res:', res);
                const user = res.data.user;
                const userInfoJson = JSON.stringify(user);
                
                that.setData({
                    userInfoJson,
                    // userInfo: user,
                    userInfo: {...that.data.userInfo, ...user},
                });
            },
            fail: console.error,
        });

    },

    onCollapseChange(event) {
        this.setData({
          activeNames: event.detail,
        });
      },

    // // 手风琴状态改变
    // onAccordionChange(e){
    //     if(this.data.currentAccordionList.length > 0){
    //         this.setData({
    //             currentAccordionList: [], 
    //         });
    //     }else{
    //         this.setData({
    //             currentAccordionList: ['0'], 
    //         });
    //     }
    // },

    // onShow(){
    //     const userInfoJson = JSON.stringify(this.data.user);
    //     this.setData({userInfoJson});
    // },

    toLinkPage(e){
        wx.navigateTo({
          url: e.currentTarget.dataset.link,
        });
    },

    // onUserInfoLinkTap(e){
    // if(getApp().globalData.userInfo === null){
    //     wx.showToast({
    //       title: '先登录才能看到个人信息哟，亲',
    //       icon: null,
    //     })
    //     return
    // }else{
    //     wx.navigateTo({
    //       url: '/page/wxUserInfo/wxUserInfo',
    //     })
    // }
    // },

    onShareAppMessage: function (res) {
        return {
          title: '自定义转发标题',
          path: '/page/me/me',
          success: function(res) {
              console.log('share success...');
            // 转发成功
          },
          fail: function(res) {
            console.log('share fail...');
            // 转发失败
          }
        }
      },

})